//
//  LimitFreeViewController.swift
//  XiaoXiangMu1604
//
//  Created by qianfeng on 16/9/26.
//  Copyright © 2016年 SunPiaoLiang. All rights reserved.
//

import UIKit

/*
限免界面
*/

class LimitFreeViewController:LFBaseViewController{
    
    private var cateId: String?
       override func viewDidLoad() {
        super.viewDidLoad()

        createMyNav()
        
        addRefresh()
        
    }
    //下载
    override func downloadData(){
        
        //进入加载状态
        ProgressHUD.showOnView(view)
        
        var urlString = String(format:kLimitUrl, curPage)
        
        if cateId != nil{
            
            //有分类
            urlString = urlString.stringByAppendingString("&category_id=\(cateId!)")
            
        }
        
        let d = LFDownloader()
        
        d.delegate = self
        
        d.downloadWithURLString(urlString)
        
    }
    //创建导航
    func createMyNav(){
        
        //分类
        addNavButton("分类",target: self, action: "gotoCategory", isLeft: true)
        
        //标题
        addNavTitle("限免")
        
        //设置
        addNavButton("设置", target: self, action: "gotoSetPage", isLeft: false)
        
        
    }
    func gotoCategory(){
        
        let categoryCrl = CategoryViewController()
        
        categoryCrl.type = .LimitFree
        
        categoryCrl.delegate = self
        
        hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(categoryCrl, animated: true)
        
        hidesBottomBarWhenPushed = false
        
        
    }
    

    func gotoSetPage(){
        
        let setCtl = SettingViewController()
        
        hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(setCtl, animated: true)
        
        hidesBottomBarWhenPushed = false
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
//MARK:LFDownloader代理
extension LimitFreeViewController:LFDownloaderDelegate{
    
    //下载失败
    func downloader(downloader: LFDownloader, didFailWithError error: NSError) {
        
        //显示下载失败的进度
        ProgressHUD.hideAfterFailOnView(view)
        
    }
    //下载成功
    func downloader(downloader: LFDownloader, didFinishWithData data: NSData) {
        
        //JSON解析
//        let str = NSString(data: data, encoding: NSUTF8StringEncoding)
//        
//        print(str!)
        
        
        
        //写在下载结束的地方---start
        if curPage == 1{
            
            //清空数组
            dataArray.removeAllObjects()
            
        }
        //写在下载结束的地方---end
        
        
        let resule = try! NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
        
        if resule.isKindOfClass(NSDictionary){
            
            let dict = resule as! Dictionary<String,AnyObject>
            
            let array = dict["applications"] as! Array<Dictionary<String,AnyObject>>
            
            for appDict in array{
                
                //创建模型对象
                let model = LimitModel()
                
                //使用KVC
                model.setValuesForKeysWithDictionary(appDict)
                
               dataArray.addObject(model)
                
            }
            
            //回到主线程刷新UI
            dispatch_async(dispatch_get_main_queue(), {
                
                self.tbView!.reloadData()
                
                //刷新结束
                self.tbView?.headerView?.endRefreshing()
                
                self.tbView?.footerView?.endRefreshing()
                
                ProgressHUD.hideAfterSuccessOnView(self.view)
                
            })
            /**
            怎么判断是滑到了最后一页
            1、接口返回一个最大页数值或者最大数量值
            1)maxPage
            2)totalCount
            
            2、http://1000phone.net:8088/app/iAppFree/api/topic.php?page=1&number=21
            
            //每页20条
            //请求21条数据，只显示20条
            
            1）如果总数是40
            1->21
            21->40   返回的数据<21条
            
            2) 如果是41条
            1->21
            21->41
            41-> 返回一条 < 21条
            
            */
        }
        
    }
    
}
//MARK:UITabelView的代理
extension LimitFreeViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
        
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 100
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cellId = "limitCellId"
        
        var cell = tbView?.dequeueReusableCellWithIdentifier(cellId) as? LimitCell
        
        if cell == nil{
            
            cell = NSBundle.mainBundle().loadNibNamed("LimitCell", owner: nil, options: nil).last as? LimitCell
            
        }
        
        cell?.selectionStyle = .None
        
        //显示数据
        let model = dataArray[indexPath.row]
        
        cell?.config(model as! LimitModel,atIndex:indexPath.row+1)
        
        return cell!
        
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        //点击进入详情界面
        let detailCtl = DetailViewController()
        
        let model = dataArray[indexPath.row] as! LimitModel
        
        detailCtl.appId = model.applicationId
        
        //跳转之前隐藏tabbar
        hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(detailCtl, animated: true)
        
        //跳转之后前界面要显示tabbar
        hidesBottomBarWhenPushed = false
        
    }
    
}
//MARK:CAtegoryViewController代理方法
extension LimitFreeViewController:CategoryViewControllerDelegate{
    
    func didClickCate(cateId: String, cateName: String) {
        
        //1.标题文字
        var title = "限免-" + cateName
        
        if cateName == "全部"{
          
            title = "限免"
            
        }
        
        addNavTitle(title)
        
        //2.重新下载
        self.cateId = cateId
        
        curPage = 1
        
        downloadData()
        
    }
    
    
    
}